---
import Badge from '~/components/Badge.astro';
import { getLatestNPMVersion } from '~/util/getLatestNPMVersion';
export interface Props {
	pkg?: string;
	v: string;
}

const { v, pkg = 'astro' } = Astro.props as Props;

/**
 * Split a semantic version string like `0.23.3` into a tuple of `[major, minor, patch]`.
 */
const parseSemVer = (semver: string) =>
	semver.split('.').map((part) => parseInt(part.replace(/[^0-9]/g, ''), 10));

/**
 * Decide a feature is “new” if it was added in the latest minor version.
 * For example, `@version 0.24.0` will be new as long as `astro@latest` is 0.24.x
 */
const getFeatureStatus = async (sinceVersion: string): Promise<'beta' | 'new' | 'current'> => {
	const latestVersion = await getLatestNPMVersion(pkg);
	const [sinceMajor, sinceMinor] = parseSemVer(sinceVersion);
	const [latestMajor, latestMinor] = parseSemVer(latestVersion);
	if (sinceMajor > latestMajor) {
		return 'beta';
	}
	if (sinceMajor === latestMajor) {
		if (sinceMinor > latestMinor) return 'beta';
		if (sinceMinor === latestMinor) return 'new';
	}
	return 'current';
};

const featureStatus = await getFeatureStatus(v);
---

<span>
	<strong>{Astro.locals.t('since.addedIn')}</strong>
	<code>{pkg}@{v}</code>
	{featureStatus === 'new' && <Badge text={Astro.locals.t('since.new')} />}
	{featureStatus === 'beta' && <Badge text={Astro.locals.t('since.beta')} />}
</span>
